package mya.mes.mdc.model.tag.convert;

import mya.mes.mdc.model.excel.ExcelTagDef;
import mya.mes.mdc.model.tag.MdcTag;

import java.util.ArrayList;
import java.util.List;

class MdcTagBuilder {


    public List<MdcTag> build(String shop, String area, List<ExcelTagDef> rowList) {
        if (rowList == null || rowList.isEmpty()) {
            return null;
        }
        List<MdcTag> mdcTagList = new ArrayList<>(rowList.size());
        PlcTagParser plcTagParser = new PlcTagParser();
        int i = 0;
        for (ExcelTagDef row : rowList) {
            i++;
            if (i == 370) {
                System.out.print("到了");
            }
            MdcTag mdcTag = convertFrom(shop, area, row, plcTagParser);

            mdcTag.shop = shop;
            mdcTag.area = area;

            mdcTagList.add(mdcTag);
        }

        return mdcTagList;
    }

    private MdcTag convertFrom(String shop, String area, ExcelTagDef row, PlcTagParser plcTagParser) {
        if (row.plcAddress == null) {
            throw new RuntimeException("PLC address == null");
        }
        MdcTagTmp mdcTagTmp = plcTagParser.parse(shop, area, row.plcAddress);
        if (mdcTagTmp == null) {
            throw new RuntimeException("mdcTagTmp == null:" + row.plcAddress);
        }
//    if(mdcTagTmp.type.equals("_MOBIL_BELT_CA_DB"))
//    {
//      System.out.println(mdcTagTmp.type);
//    }
        mdcTagTmp.type = mdcTagTmp.type.replace('.', '_');
        System.out.println(mdcTagTmp.type);

        MdcTag mdcTag = new MdcTag();
        setMdcTag(mdcTag, row.tagName);

        mdcTag.viewTagName = mdcTagTmp.viewTag;
        mdcTag.plcTagType = mdcTagTmp.type;

        mdcTag.dataType = row.dataType;
        mdcTag.plcAddress = row.plcAddress;

        mdcTag.description = row.description;

        if (PlcTagType._START_RUNNING.equals(mdcTag.plcTagType)) {
            mdcTag.startRunningTag = true;
        }

        if (PlcTagType._STOP_RUNNING.equals(mdcTag.plcTagType)) {
            mdcTag.stopRunningTag = true;
        }

        if (PlcTagType._PROD.equals(mdcTag.plcTagType)) {
            mdcTag.statusProdTag = true;
        }

        if (PlcTagType._PRODUCTION.equals(mdcTag.plcTagType)) {
            mdcTag.statusProdTag = true;
        }

        if (PlcTagType._MAINTENANCE.equals(mdcTag.plcTagType)) {
            mdcTag.statusMaintTag = true;
        }

        if (PlcTagType.BFG1_MANUAL.equals(mdcTag.plcTagType) ||
                PlcTagType.BFG2_MANUAL.equals(mdcTag.plcTagType) ||
                PlcTagType.BFG3_MANUAL.equals(mdcTag.plcTagType) ||
                PlcTagType.BFG4_MANUAL.equals(mdcTag.plcTagType)) {
            mdcTag.statusMaintTag = true;
        }


        if (PlcTagType.BFG1_GREEN.equals(mdcTag.plcTagType) ||
                PlcTagType.BFG2_GREEN.equals(mdcTag.plcTagType) ||
                PlcTagType.BFG3_GREEN.equals(mdcTag.plcTagType) ||
                PlcTagType.BFG4_GREEN.equals(mdcTag.plcTagType)) {
            mdcTag.statusProdTag = true;
        }


        for (String tagName : PlcTagType.digitsTagArray) {
            if (tagName.equals(mdcTag.plcTagType)) {
                mdcTag.digitsTag = true;
                break;
            }
        }

        if (!mdcTag.digitsTag) {
            for (String tagName : PlcTagType.posteTagArray) {
                if (tagName.equals(mdcTag.plcTagType)) {
                    mdcTag.posteTag = true;
                    break;
                }
            }
        }

        return mdcTag;
    }


    private void setMdcTag(MdcTag mdcTag, String tagNameExcel) {

        if (tagNameExcel == null) {
            throw new IllegalArgumentException("tagName in Excel不能为null");
        }
        tagNameExcel = tagNameExcel.trim();
        if (tagNameExcel.isEmpty()) {
            throw new IllegalArgumentException("tagName in Excel不能为空");
        }
        mdcTag.tagName = tagNameExcel;
        mdcTag.tagPrefix = mdcTag.tagName.substring(0, 2);
        try {
            mdcTag.tagIndex = Integer.parseInt(tagNameExcel.substring(2));
        } catch (Exception e) {
            throw new IllegalArgumentException("tagName in Excel: " + tagNameExcel);
        }

    }


}
